set more off

use "dataset_illinois", clear

local X policyself income age ideology ideology2 party

local j = 1

forvalues k = 15(3)27 {

	rename q`k'_1_text race1`j'
	rename q`k'_2_text policy1`j'
	rename q`k'_3_text neighbor1`j'
	rename q`k'_4_text experience1`j'

	rename q`k'_5_text race2`j'
	rename q`k'_6_text policy2`j'
	rename q`k'_7_text neighbor2`j'
	rename q`k'_8_text experience2`j'

	rename q`k'_9_text order`j'
	
	local j = `j' + 1

}

local j = 1

forvalues i = 14(3)26 {

	rename q`i' choice`j'
	destring choice`j', replace
	
	gen choice1`j' = 1 if choice`j' == 1
	replace choice1`j' = 0 if choice`j' == 2
	
	gen choice2`j' = 1 if choice`j' == 2
	replace choice2`j' = 0 if choice`j' == 1
	
	drop choice`j'
	
	local j = `j' + 1
	
}

keep V1 race* policy* neighbor* experience* order* choice* `X'

reshape i V1
reshape j pair
reshape xij race1 race2 policy1 policy2 neighbor1 neighbor2 experience1 experience2 order choice1 choice2
reshape xi `X'
reshape long

keep if pair < 3

egen id = group(V1 pair)
reshape i id
reshape j candidate
reshape xij race policy neighbor experience choice
reshape xi V1 order pair `X'
reshape long

replace policy = "1 " + "Supports cuts" if regexm(policy, "Supports cut")
replace policy = "2 " + "Supports maintaining" if regexm(policy, "Supports maintain")
replace policy = "3 " + "Supports increase" if regexm(policy, "Supports increas")

replace experience = " 2 years" if experience == "2 years"
replace experience = " 5 years" if experience == "5 years"
replace experience = experience + " experience"

replace policyself = ideology2 
encode policy, gen(policyc)
replace policyc = (policyc - 1)/(2)
gen proximity = -(abs(policyself - policyc))
drop policyc

tostring proximity, replace
drop if prox == "."
replace proximity = "-1/2"  if prox == "-.5"
replace prox = prox + " issue distance"

preserve

	gen index = _n
	local k = 1
	foreach x of varlist race proximity neighbor experience {

		xi: reg choice i.`x', cl(V1)
		mat B`k' = e(b)
		svmat B`k'
		mat varr = vecdiag(e(V))
		matmap varr SE`k' , m(sqrt(@))
		svmat SE`k'
		
		local m = 1
		egen nvalues`k' = nvals(`x')
		levelsof `x', local(levels)
		encode `x', gen(`x'c)
		foreach l of local levels {
			if `m' != nvalues`k' {
				gen index2 = 1 if `x'c == `m' + 1
				sort index2
				gen label`k'`m' = `x'[1]
				local m = `m' + 1
				drop index2
			}
			if `m' == nvalues`k' {
				gen index2 = 1 if `x'c == 1
				sort index2
				gen label`k'`m' = `x'[1]
				local m = `m' + 1
				drop index2
			}
		}
		
		local k = `k' + 1
		
	}

	l B11 label11 if !mi(B11)

	keep B* SE* label*
	gen index = _n
	reshape i index
	reshape j value 1 2 3 4
	reshape xij B1 B2 B3 B4 SE1 SE2 SE3 SE4 label1 label2 label3 label4
	reshape xi 
	reshape long
	drop index
	gen index = _n

	reshape i index
	reshape j variable
	reshape xij B SE label
	reshape long

	keep if !mi(B)

	egen maxval = max(value), by(variable)
	replace B = 0 if value == maxval

	replace value = 0 if value == maxval

	drop index

	sort variable value

	gen index = _n

	gen UB = B + 1.96*SE if value != 0
	gen LB = B - 1.96*SE if value != 0

	labmask index, values(label)

	#delimit;

	gr tw 
		(sc index B if variable != 2, col(black)) 
		(rspike UB LB index if variable, horiz col(black))
		(sc index B if variable == 2, col(black) msym(O) mfcol(white)) 
		,
			yscale(rev)
			ylab(1(1)13, valuelabel angle(horiz))
			xline(0)
			xtitle("Marginal effect")
			legend(off)
 			name(g`j', replace)
			ytitle("")
			plotregion(style(none))
			;
			
	#delimit cr
	
restore

gr export "figure A8.eps", replace
shell epstopdf "figure A8.eps"

